package com.iconmaker.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.iconmaker.model.Conf;

public class PixcelView extends View {
	private int backgroundColor = Color.WHITE;
	private Paint paint = new Paint();
	private int width = 100;
	private int height = 100;
	private int margin = 1;
	
	public void setMargin(int margin) {
		this.margin = margin;
	}
	
	public PixcelView(Context context) {
		super(context);
	}
	
	public PixcelView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public PixcelView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
//		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
//		
//		int width = (int)getDip(10);
//		int height = (int)getDip(10);
		
//		switch(widthMode) {
//			case MeasureSpec.UNSPECIFIED : // unspecified  
//				width = 10;
//				break;
//			case MeasureSpec.AT_MOST : // wrap_content  
//				break;
//			case MeasureSpec.EXACTLY : // match_parent  
//				width = MeasureSpec.getSize(widthMeasureSpec);
//				break;
//		}
//		
//		switch(heightMode) {
//			case MeasureSpec.UNSPECIFIED : // unspecified  
//				height = 10;
//				break;
//			case MeasureSpec.AT_MOST : // wrap_content  
//				break;
//			case MeasureSpec.EXACTLY : // match_parent  
//				height = MeasureSpec.getSize(heightMeasureSpec);
//				break;
//		}
		
		setMeasuredDimension((int)getDip(width), (int)getDip(height));
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		paint.setColor(backgroundColor);
		
		canvas.clipRect(margin, margin, getWidth() - (margin * 2), getHeight() - (margin * 2));
		canvas.drawRect(0.0F, 0.0F, getWidth(), getHeight(), paint);
	}
	
	@Override
	public void setBackgroundColor(int color) {
		backgroundColor = color;
				
		invalidate();
	}
	
	public int getBackgroundColor() {
		return backgroundColor;
	}
	
//	@Override
//	public boolean onTouchEvent(MotionEvent event) {
//		backgroundColor = Color.WHITE;
//		
//		invalidate();
//		return super.onTouchEvent(event);
//	}
//	
	public float getDip(float value) {
		return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, getResources().getDisplayMetrics());
	}
	
	public void setSize(int width, int height) {
		this.width = width;
		this.height = height;
		Conf.pixcelDimensionSize = (int)getDip(width);
	}
}
